//
//  ST_BootQQApiHandle.swift
//  btvideo
//
//  Created by Jason on 2021/11/1.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import Foundation
import SwiftyJSON


class ST_BootQQApiHandle:NSObject{
    
    fileprivate var qqAuth:TencentOAuth?
    
    func start_tencent_o_auth() {
        qqAuth = TencentOAuth.init(appId: "***", andDelegate: self)
    }
    
    func authorize_q_q() {
        
//        qqAuth?.authorize(["get_user_info","get_simple_userinfo"])
        
        qqAuth?.authorize(["get_simple_userinfo"])
        
    }
    
}
extension ST_BootQQApiHandle:TencentSessionDelegate,QQApiInterfaceDelegate{
    
    func isOnlineResponse(_ response: [AnyHashable : Any]!) {
        
    }
    
    
    func onReq(_ req: QQBaseReq!) {
        //        print("QQ---onReq----11----",req)
    }
    
    func onResp(_ resp: QQBaseResp!) {
        //        print("QQ---onResp----22----",resp)
    }
    
    func getUserInfoResponse(_ response: APIResponse!) {
        
                print("----------------------------------------")
                print("【bug QQ登录多次创建对象bug】 \(Date())QQ用户资料获取成功：")
//                print(response.jsonResponse)
        
        var json = JSON(response.jsonResponse ?? "").dictionaryObject
        
        guard let openId = qqAuth?.openId else { return  }
        
        json?.updateValue(openId, forKey: "openId")
        
        NotificationCenter.default.post(name: Notification.Name.q_q_login_finished, object: nil, userInfo: json)
        
    }
    
    
    func tencentDidLogin() {
        
        
        if !(qqAuth?.accessToken.isEmpty ?? false) {
                        qqAuth?.getUserInfo()
        }
        
        
        #if DEBUG
        if !(qqAuth?.accessToken.isEmpty ?? false) {
                   print("----------------------------------------")
                   print("登录成功！")
            
//            print("openId：\(qqAuth?.openId)",
//                  "accessToken：\(qqAuth?.accessToken)",
//                  "expirationDate：\(qqAuth?.expirationDate)")
                    print("开始获取用户资料")
//            qqAuth?.getUserInfo()
            
           }else {
               print("登录失败！没有获取到accessToken")
           }
        
        #endif
        
        
        
    }
    
    func tencentDidNotLogin(_ cancelled: Bool) {
        
    }
    
    func tencentDidNotNetWork() {
        
    }
    
    
    
    
}
